﻿(function (_this) {

    _this.Data = (function () {
        var __this = new Object();
        function getConfig() {
            return {
                data: {},
                param: {},
                pagesize: 10,
                pageindex: 1,
                type: "POST",
                async: true,
                cache: false,
                dataType: "text",
                url: SiteConfig.FrameworkServiceUrl + "Main.ashx",
                success: function (ev) { },
                error: function (ev) { }
            };
        }

        __this.downloadfile = function (fileurl, filename) {
            filename = filename || '';
            location.href = SiteConfig.FrameworkServiceUrl + "Download.ashx?fileurl=" + encodeURIComponent(fileurl) + "&filename=" + encodeURIComponent(filename);
        };

        
        function requestData(_config) {
            _config.data['_datatype'] = _config.dataType;
            _config.data['_dataid'] = _config.dataid;
            _config.data['_param'] = _config.param;
            $.ajax({
                type: _config.dataType == 'jsonp' ? "GET" : "POST",
                url: _config.url,
                async: _config.async,
                cache: _config.cache,
                data: _config.dataType == 'jsonp' ? { data: JSON.stringify(_config.data) } : JSON.stringify(_config.data),
                dataType: _config.dataType || 'text',
                success: function (result) {
                    if (result && result != '') {
                        if (result && result != '') {
                            if (typeof (result) == "object") {
                                _config.success(result)
                            } else {
                                while (result.match(/\r/) || result.match(/\n/)) {
                                    result = result.replace(/\r/, "").replace(/\n/, "")
                                }
                                if (_config.dataType && _config.dataType.toLowerCase() == "text") {
                                    
                                    _config.success(eval("(" + result + ")") || result)
                                } else {
                                    _config.success(eval("(" + result + ")") || result)
                                }
                            }
                        }
                    }
                },
                error: function (err, e, a) {
                    console.log('error: function (err, e, a)');
                    console.log(err);
                    console.log(e);
                    console.log(a);
                    if (e == "error") {
                        console.log("网络服务器解析错误");
                    }
                    console.log(err.responseText)
                    _config.error(err, e, a);
                }
            })
        };
        __this.getData = function (options) {
            var _config = getConfig();
            var _config = $.extend(true, _config, options);
            _config.data['_type'] = "getnormal";
            requestData(_config);
        };
        __this.getListData = function (options) {
            var _config = getConfig();
            var _config = $.extend(true, _config, options);
            _config.data['_type'] = "getlist";
            requestData(_config);
        };
        __this.getListPageData = function (options) {
            var _config = getConfig();
            var _config = $.extend(true, _config, options);
            _config.data['_type'] = "getlistpage";
            _config.data['_pagesize'] = _config.pagesize;
            _config.data['_pageindex'] = _config.pageindex;
            requestData(_config);
        };
        __this.setData = function (options) {
            var _config = getConfig();
            var _config = $.extend(true, _config, options);
            _config.data['_type'] = "setnormal";
            requestData(_config);
        };
        __this.setMultiData = function (options) {
            var _config = getConfig();
            var _config = $.extend(true, _config, options);
            _config.data['_type'] = "setmulti";
            requestData(_config);
        };
        __this.getFuncData = function (options) {
            var _config = getConfig();
            var _config = $.extend(true, _config, options);
            // _config.url = _config.url + "?option=func&funcid=" + _config["funcid"];
            _config.url = vui.Url.parentPath() + SiteConfig.Api.Sys[_config["funcid"]];
            // _config.url = _config["funcid"];
            requestData(_config);
        };
        return __this;
    })();
})(vui);
